package com.thx.common.service;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.orm.PaginationDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * Service 层基础类.
 * 
 * @author balance
 */
@Transactional(readOnly = true)
public class BaseManager<T> {

  @Autowired
  protected PaginationDao<T> paginDao;

  @Autowired
  protected HibernateBaseDao<T> baseDao;

  public List<T> paginList(int start, int max) {
    List<T> list = paginDao.findByPagination(start, max);
    return list;
  }

  public int paginCount() {
    int count = paginDao.countByPagination();
    return count;
  }

  @Transactional(readOnly = false)
  public void save(T entity) {
    baseDao.saveOrUpdate(entity);
  }

  @Transactional(readOnly = false)
  public void update(T entity) {
    save(entity);
  }

  @Transactional(readOnly = false)
  public void delete(Serializable id) {
    baseDao.delete(id);
  }

  /**
   * 删除多个对象.
   * 
   * @param ids 对象 id
   */
  @Transactional(readOnly = false)
  public void delMulti(Serializable... ids) {
    if (ids != null && ids.length > 0) {
      for (Serializable id : ids) {
        delete(id);
      }
    }
  }

  public T get(Serializable id) {
    T entity = (T) baseDao.get(id);
    return entity;
  }

  public List<T> getAll() {
    return baseDao.getAll();
  }

}
